package com.dispatcher;


import java.util.ArrayList;
import java.util.List;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import cn.jpush.android.api.JPushInterface;

import com.dispatcher.helper.SDHelper;

interface IConnectionPoint{
	public void addPoint(IRefreshListener listener);
	public void deletePoint(IRefreshListener listener);
}
interface IUserData{
	public void setUserid(String userid);
	public void setListid(String listid);
	public void setIds(String ids);
}
interface IRefreshListener{
	public void onNewAtMeMsg(int count);
	public void onNewCommentMeMsg(int count);
	public void onNewLetterMsg(int count);
}

interface IRequestUnreadMsgCount{
	public void onRequestUnreadMsgCount();
}

public class LocalService extends Service{
	private List<IRefreshListener> mlisteners=new ArrayList<IRefreshListener>();
	private boolean threadDisable;
	private boolean mbfirst=true;
	private boolean mbgetfriend=false;
	String muserid;
	String mids;
	String mlistid;
	String mcitycode;
	String msex;
	
	private ServiceBinder serviceBinder = new ServiceBinder();
	
	public class ServiceBinder extends Binder implements IConnectionPoint,IUserData, IRequestUnreadMsgCount{
	    @Override
		public void addPoint(IRefreshListener listener) {
	    	mlisteners.add(listener);
	    }
	    @Override
		public void deletePoint(IRefreshListener listener) {
	    	mlisteners.remove(listener);
	    }
	    @Override
		public void setUserid(String userid){
	    	muserid=userid;
	    }
	    @Override
		public void setIds(String ids){
	    	mids=ids;
	    }
	    @Override
		public void setListid(String listid){
	    	mlistid=listid;
	    	mbfirst=true;
	    	mbgetfriend=false;
	    }
	    
	    @Override
	    public void onRequestUnreadMsgCount() {
//	    	new Thread(new Runnable() {
//				
//				@Override
//				public void run() {
//					Looper.prepare();
//					requestUnreadMsgCount();
//					
//				}
//			}).start();
	    }
		
	}
	@Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return START_STICKY;
    }
	@Override
	public IBinder onBind(Intent intent) {
	    return serviceBinder;
	}
	
	@Override
	public void onCreate() {
	    super .onCreate();
	    setForeground(true);
	    String token = getToken();
		Log.i("liutao123", "token: " + token);
		JPushInterface.setDebugMode(true); 	//设置开启日志,发布时请关闭日志
        JPushInterface.init(this);     		// 初始化 JPush
//        JPushInterface.setAliasAndTags(this, "liu", null);
        if(token != null) {
        	JPushInterface.setAliasAndTags(this, "saler_"+token, null);
        	
        }
//	    new Thread(new Runnable() {
//			
//			@Override
//			public void run() {
//				Looper.prepare();
//				requestFriend();
//				
//			}
//		}).start();
	}
	
	

	@Override
	public void onDestroy() {
	    super .onDestroy();
	    this.threadDisable = true ;
//	    Intent localIntent = new Intent();
//        localIntent.setClass(this, LocalService.class);  //销毁时重新启动Service
//        this.startService(localIntent);
	}
	
	
	
	public String getToken() {
		String token = SDHelper.getPresentId();
		return token;
	}
}
